import 'package:flutter/material.dart';
import 'package:x3_charts_core/chart_painter/right_stacked_histogram_chart_painter.dart';
import 'package:x3_charts_core/model/right_stacked_histogram_chart_model.dart';

///**
///* @author hrongyong
///* @date   3/5/21
///* @email hrongyong@gmail.com
///* Description:  RightStackedHistogramChartWidget 右堆积直方图
///*/
class RightStackedHistogramChartWidget extends StatefulWidget {
  ///背景颜色
  final Color backgroundColor;

  ///Y轴示例 - 用于测量Y轴系数宽度
  final String yAxisExample;

  ///X轴最大值
  final int xAxisMax;

  ///图表数组
  final List<RightStackedHistogramChartModel> chartList;

  ///绘制X轴线
  final bool drawXAxis;

  ///绘制X轴线刻度
  final bool drawXAxisScale;

  ///绘制Y轴线
  final bool drawYAxis;

  ///绘制Y轴线刻度
  final bool drawYAxisScale;

  ///显示几个横条
  final int showSeveralBars;

  ///默认颜色
  final Color defaultColor;

  RightStackedHistogramChartWidget({
    @required this.chartList,
    this.backgroundColor,
    @required this.yAxisExample,
    @required this.xAxisMax,
    this.drawXAxis = false,
    this.drawXAxisScale = true,
    this.drawYAxis = true,
    this.drawYAxisScale = true,
    this.showSeveralBars,
    this.defaultColor = Colors.red,
  });

  @override
  _RightStackedHistogramChartWidgetState createState() => _RightStackedHistogramChartWidgetState();
}

class _RightStackedHistogramChartWidgetState extends State<RightStackedHistogramChartWidget> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(milliseconds: 1000),
      vsync: this,
    )..forward();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return CustomPaint(
          size: Size(constraints.maxWidth, constraints.maxHeight),
          painter: RightStackedHistogramChartPainter(
            chartList: widget.chartList,
            repaint: _controller,
            backgroundColor: widget.backgroundColor,
            yAxisExample: widget.yAxisExample,
            xAxisMax: widget.xAxisMax,
            drawXAxis: widget.drawXAxis,
            drawXAxisScale: widget.drawXAxisScale,
            drawYAxis: widget.drawYAxis,
            drawYAxisScale: widget.drawYAxisScale,
            showSeveralBars: widget.showSeveralBars,
            defaultColor: widget.defaultColor,
          ),
        );
      },
    );
  }
}
